﻿//////////////////////////////////////////////////////////////////////////////
//
//  Copyright © 1998-2024 Glodon Company Limited.  All rights reserved.
//
//  Use of this software is subject to the terms of the Glodon license
//  agreement provided at the time of installation or download, or which
//  otherwise accompanies this software in either electronic or hard copy form.  
//
//////////////////////////////////////////////////////////////////////////////

#pragma  once
#include "GnufAssistant.h"
#include <QFrame>
#include "GnufINotificationCenterEventHandler.h"

class QPushButton;
class QLabel;

namespace gnuf
{
    class INotificationEventArgs;
}

class GNUF_ASSISTANT_EXPORT GnufQtDialogHeader 
    : public QFrame
    , public gnuf::INotificationCenterEventHandler
{
    Q_OBJECT

public:
    explicit GnufQtDialogHeader(QWidget* parent = 0,std::wstring objectNamePrefix=L"");
    ~GnufQtDialogHeader();

    enum class EButtonType : std::uint8_t {
        eMinimize,
        eMaximize,
        eRestore,
        eClose
    };

    /// \brief 设置右侧图标的可见性
    void setIconVisible(bool visible);

    /// \brief 设置标题内容
    void setText(const QString& text);

    /// \brief 设置标题的可见性
    void setTextVisible(bool visible);

    /// \brief 设置最小化按钮的可见性
    void setMinButtonVisible(bool visible);

    /// \brief 设置最大化按钮的可见性
    void setMaxButtonVisible(bool visible);

    /// \brief 设置恢复大小按钮的可见性
    void setRestoreButtonVisible(bool visible);

    /// \brief 设置关闭按钮的可见性
    void setCloseButtonVisible(bool visible);

    /// \brief 设置按钮objectName的前缀
    void setButtonObjectNamePrefix(const std::wstring& prefix);

    /// \brief 添加Widget
    /// \param widget 即将添加的widget
    /// \param stretch 伸缩系数
    /// \param alignment 对齐方式
    /// \param at_end 插入的位置，true表示在标题文字后插入，false表示在标题图标前插入
    void addWidget(QWidget* widget, int stretch = 0, Qt::Alignment alignment = 0,
                   bool at_end = true);

    /// \brief 移除指定的Widget
    void removeWidget(QWidget* widget);

    /// \brief 替换图标
    /// \param iconPaths 按钮类型与图标路径的映射关系，图标路径按Gnuf风格提供
    void replaceIcons(const std::unordered_map< EButtonType, std::wstring > & iconPathes);
    void replaceIcon(const EButtonType, const std::wstring & iconPath);

signals:
    void minimizeRequested();
    void normalRequested();
    void maximizeRequested();
    void closeRequested();
    void moveRequested(const QPoint& cursorPos, const QPoint& offset);

protected:
    // from QFrame
    void mousePressEvent(QMouseEvent* e) Q_DECL_OVERRIDE;
    void mouseMoveEvent(QMouseEvent* e) Q_DECL_OVERRIDE;
    void mouseReleaseEvent(QMouseEvent* e) Q_DECL_OVERRIDE;
    bool event(QEvent* e) Q_DECL_OVERRIDE;

    // from INotificationCenterEventHandler
    void onEvent(gnuf::INotificationCenterEventArgs* pArgs) override;

private:
    /// \brief 刷新按钮的样式
    void refreshButtonIcon(EButtonType buttonType);

    void refreshButtonIcon();

    /// \brief 设置标题栏的总体样式
    void setStyle();

private:
    QLabel* m_pIconLabel;
    QLabel* m_pTextLabel;

    std::wstring m_buttonObjectNamePrefix;

    QPushButton* m_pMinButton;
    std::wstring m_minButtonIcon;
    QPushButton* m_pMaxButton;
    std::wstring m_maxButtonIcon;
    QPushButton* m_pRestoreButton;
    std::wstring m_restoreButtonIcon;
    QPushButton* m_pCloseButton;
    std::wstring m_closeButtonIcon;

    bool m_bPressed;
    QPoint m_pressPos;
    QPoint m_moveStartPos;
};


